<template>
  <div class="preview-area">
    <div class="preview-header">
      <div class="preview-tabs">
        <span class="tab active">微官网</span>
        <span class="tab">PC端官网</span>
      </div>
    </div>
    
    <div class="device-preview" @dragover="handleDragOver" @drop="handleDrop">
      <div class="phone-frame">
        <div class="phone-header">
          <div class="camera"></div>
        </div>
        <div class="phone-screen">
          <!-- 预览内容 -->
          <div class="preview-content">
            <!-- 空状态 -->
            <div v-if="addedComponents.length === 0" class="empty-state">
              <i class="el-icon-upload"></i>
              <p>拖拽左侧组件到此处</p>
            </div>
            
            <!-- 已添加的组件 -->
            <div v-for="(component, index) in addedComponents" 
                 :key="component.id" 
                 class="added-component" 
                 :class="{selected: selectedComponent && selectedComponent.id === component.id}"
                 :data-type="component.type"
                 :data-index="index"
                 @click="handleComponentClick(component)"
                 draggable="true"
                 @dragstart="handleComponentDragStart($event, component, index)"
                 @dragover="handleComponentDragOver($event, index)"
                 @drop="handleComponentDrop($event)"
                 @dragend="handleComponentDragEnd">
              <!-- 拖拽指示线 -->
              <div v-if="dragOverIndex === index" class="drag-indicator"></div>
              
              <div class="component-header">
                <div class="component-title">{{ component.label }}</div>
                <button class="delete-btn" @click.stop="handleDeleteComponent(index)">
                  <i class="el-icon-delete"></i>
                </button>
              </div>
              
              <div class="component-content">
                <!-- 根据组件类型显示不同的预览 -->
                <div v-if="component.type === 'text'" class="text-preview" 
                     :style="{
                       backgroundColor: component.bgColor || 'transparent',
                       borderStyle: component.borderStyle,
                       borderColor: component.borderColor,
                       borderRadius: component.borderRadius + 'px',
                       paddingTop: component.paddingTop + 'px',
                       paddingRight: component.paddingRight + 'px',
                       paddingBottom: component.paddingBottom + 'px',
                       paddingLeft: component.paddingLeft + 'px',
                       textAlign: component.align,
                       fontFamily: component.fontFamily,
                       fontSize: component.fontSize,
                       color: component.color,
                       fontWeight: component.bold ? 'bold' : 'normal',
                       fontStyle: component.italic ? 'italic' : 'normal',
                       textDecoration: component.underline ? 'underline' : 'none'
                     }">
                  {{ component.content || '请输入文本内容' }}
                </div>
                <div v-else-if="component.type === 'image'" class="image-preview">
                  <div v-if="!component.imageUrl" class="image-placeholder"></div>
                  <img v-else :src="component.imageUrl" alt="预览图片" style="max-width: 100%;">
                </div>
                <div v-else-if="component.type === 'button'" class="button-preview">
                  <button>{{ component.content || '按钮' }}</button>
                </div>
                <!-- /* 列表多图组件预览 */ -->
                <div v-else-if="component.type === 'list'" class="list-multiple-images">
                  <div class="list-items">
                    <div v-for="(item, idx) in (component.items || [1, 2, 3])" :key="idx" class="list-item">
                      <div class="list-image">{{ idx + 1 }}</div>
                      <div class="list-text">
                        <div class="list-title">{{ component.itemTitle || '项目标题' }}</div>
                        <div class="list-desc">{{ component.itemDesc || '项目描述内容' }}</div>
                      </div>
                    </div>
                  </div>
                </div>
                <!-- /* 轮播多图组件预览 */ -->
                <div v-else-if="component.type === 'carousel'" class="carousel-multiple-images">
                  <div class="carousel-container">
                    <div class="carousel-item" v-for="(item, idx) in (component.carouselItems || [1, 2, 3])" :key="idx">
                      <div class="carousel-image">{{ idx + 1 }}</div>
                      <div class="carousel-text">{{ component.carouselTitle || '轮播标题' }}</div>
                    </div>
                  </div>
                  <div class="carousel-indicators">
                    <span v-for="(item, idx) in (component.carouselItems || [1, 2, 3])" :key="idx" 
                          :class="{active: component.activeIndex === idx || idx === 0}"></span>
                  </div>
                </div>
                <!-- /* 魔方导航组件预览 */ -->
                <div v-else-if="component.type === 'grid'" class="grid-navigation">
                  <div class="grid-items">
                    <div v-for="(item, idx) in (component.gridItems || [1, 2, 3, 4, 5, 6])" :key="idx" class="grid-item">
                      <div class="grid-icon">{{ idx + 1 }}</div>
                      <div class="grid-label">{{ component.gridLabels && component.gridLabels[idx] ? component.gridLabels[idx] : '导航' + (idx + 1) }}</div>
                    </div>
                  </div>
                </div>
                <!-- /* 图文展示组件预览 */ -->
                <div v-else-if="component.type === '图文展示'" class="image-text-display">
                  <div class="image-text-content">
                    <div v-if="!component.featureImage" class="feature-image-placeholder"></div>
                    <img v-else :src="component.featureImage" alt="特色图片" class="feature-image">
                    <div class="text-content">
                      <h3>{{ component.featureTitle || '特色标题' }}</h3>
                      <p>{{ component.featureDesc || '这里是图文展示的详细描述内容...' }}</p>
                    </div>
                  </div>
                </div>
                <!-- /* 横向标签组件预览 */ -->
                <div v-else-if="component.type === 'tags'" class="horizontal-tags">
                  <div class="tags-container">
                    <span v-for="(tag, idx) in (component.tags || ['标签1', '标签2', '标签3', '标签4'])" :key="idx" class="tag-item">
                      {{ tag }}
                    </span>
                  </div>
                </div>
                <!-- /* 搜索组件预览 */ -->
                <div v-else-if="component.type === 'search'" class="search-component">
                  <div class="search-input-wrapper">
                    <input type="text" placeholder="搜索内容..." class="search-input">
                    <button class="search-button">搜索</button>
                  </div>
                </div>
                <!-- /* 公告组件预览 */ -->
                <div v-else-if="component.type === 'notice'" class="notice-component">
                  <div class="notice-icon">📢</div>
                  <div class="notice-text">
                    {{ component.noticeContent || '这里是公告内容，点击查看详情' }}
                  </div>
                </div>
                <!-- 默认预览 -->
                <div v-else class="default-preview">
                  {{ component.label }} 组件
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    
    <div class="preview-actions">
      <button class="action-btn secondary">添加二级页面</button>
    </div>
  </div>
</template>

<script>
export default {
  name: 'PreviewArea',
  props: {
    addedComponents: {
      type: Array,
      default: () => []
    },
    selectedComponent: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      dragOverIndex: -1
    };
  },
  methods: {
    // 处理组件点击
    handleComponentClick(component) {
      this.$emit('component-select', component);
    },
    
    // 处理删除组件
    handleDeleteComponent(index) {
      this.$emit('component-delete', index);
    },
    
    // 处理拖拽经过
    handleDragOver(event) {
      event.preventDefault();
      event.dataTransfer.dropEffect = 'copy';
    },
    
    // 处理组件拖拽经过
    handleComponentDragOver(event, index) {
      event.preventDefault();
      event.dataTransfer.dropEffect = 'move';
      this.dragOverIndex = index;
    },
    
    // 处理组件拖拽结束
    handleComponentDragEnd() {
      this.dragOverIndex = -1;
    },
    
    // 处理放置
    handleDrop(event) {
      this.$emit('drop', event);
      this.dragOverIndex = -1;
    },
    
    // 处理组件内放置
    handleComponentDrop(event) {
      this.$emit('component-drop', event);
      this.dragOverIndex = -1;
    },
    
    // 处理组件拖拽开始
    handleComponentDragStart(event, component, index) {
      this.$emit('component-drag-start', event, component, index);
      
      // 添加拖拽时的视觉效果
      const element = event.target;
      element.classList.add('dragging-component');
      
      // 监听拖拽结束事件移除样式
      const handleDragEnd = () => {
        element.classList.remove('dragging-component');
      };
      
      event.target.addEventListener('dragend', handleDragEnd, {once: true});
    }
  }
};
</script>

<style scoped>
.preview-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px;
  position: relative;
}

.preview-header {
  width: 100%;
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.preview-tabs {
  display: flex;
}

.tab {
  padding: 8px 20px;
  margin: 0 5px;
  cursor: pointer;
  color: #666;
  border-radius: 4px;
  transition: all 0.3s;
}

.tab.active {
  color: #1890ff;
  border-bottom: 2px solid #1890ff;
  background-color: rgba(24, 144, 255, 0.05);
}

.preview-info {
  font-size: 12px;
  color: #666;
  background-color: #fff;
  padding: 5px 10px;
  border-radius: 4px;
  border: 1px solid #e8e8e8;
}

.device-preview {
  width: 300px;
  height: 600px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #fff;
  border-radius: 10px;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
  position: relative;
}

.device-preview.drag-over {
  background-color: rgba(24, 144, 255, 0.05);
  border: 2px dashed #1890ff;
}

.phone-frame {
  width: 260px;
  height: 550px;
  border: 10px solid #333;
  border-radius: 30px;
  position: relative;
  overflow: hidden;
  background-color: #fff;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
}

.phone-header {
  height: 40px;
  background-color: #333;
  position: relative;
}

.camera {
  width: 12px;
  height: 12px;
  background-color: #555;
  border-radius: 50%;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.phone-screen {
  height: calc(100% - 40px);
  overflow-y: auto;
  position: relative;
}

.preview-content {
  padding: 15px;
  min-height: 100%;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 400px;
  color: #999;
  text-align: center;
}

.empty-state i {
  font-size: 48px;
  margin-bottom: 10px;
  opacity: 0.5;
}

.added-component {
  margin-bottom: 15px;
  padding: 15px;
  background-color: #fafafa;
  border: 1px dashed #d9d9d9;
  border-radius: 8px;
  transition: all 0.3s;
  position: relative;
  cursor: move;
}

.added-component:hover {
  border-color: #1890ff;
  background-color: #f0f7ff;
}

.added-component.selected {
  border-color: #1890ff;
  border-style: solid;
  background-color: #e6f7ff;
  box-shadow: 0 0 10px rgba(24, 144, 255, 0.2);
}

.added-component.dragging-component {
  opacity: 0.5;
  transform: rotate(2deg);
}

.component-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
  padding-bottom: 5px;
  border-bottom: 1px solid #e8e8e8;
}

.component-title {
  font-size: 14px;
  font-weight: 500;
  color: #333;
}

.delete-btn {
  background: none;
  border: none;
  font-size: 14px;
  color: #999;
  cursor: pointer;
  padding: 2px 5px;
  border-radius: 3px;
  transition: all 0.3s;
}

.delete-btn:hover {
  color: #ff4d4f;
  background-color: rgba(255, 77, 79, 0.1);
}

.component-content {
  min-height: 40px;
}

/* 组件预览样式 */
.text-preview {
  font-size: 14px;
  line-height: 1.6;
  color: #333;
  background-color: #f9f9f9;
  border: 1px solid #e8e8e8;
  border-radius: 4px;
  padding: 15px;
  min-height: 60px;
  display: block;
  word-wrap: break-word;
}

.text-preview.bold {
  font-weight: bold;
}

.text-preview.italic {
  font-style: italic;
}

.text-preview.underline {
  text-decoration: underline;
}

.image-preview {
  display: flex;
  justify-content: center;
}

.image-placeholder {
  width: 100%;
  height: 120px;
  background-color: #f5f5f5;
  border: 1px dashed #d9d9d9;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #999;
}

.image-placeholder::after {
  content: "图片区域";
}

.button-preview {
  display: flex;
  justify-content: center;
  padding: 10px 0;
}

.button-preview button {
  padding: 6px 16px;
  background-color: #1890ff;
  color: #fff;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
}

.default-preview {
  text-align: center;
  padding: 20px;
  color: #666;
  font-size: 14px;
}

/* 拖拽指示线 */
.drag-indicator {
  position: absolute;
  left: 0;
  right: 0;
  height: 2px;
  background-color: #1890ff;
  top: -5px;
  box-shadow: 0 0 5px rgba(24, 144, 255, 0.5);
  animation: pulse 1s infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

.preview-actions {
  margin-top: 20px;
}

.action-btn {
  padding: 10px 20px;
  border: none;
  border-radius: 4px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s;
}

.action-btn.secondary {
  background-color: #fff;
  color: #666;
  border: 1px solid #d9d9d9;
}

.action-btn.secondary:hover {
  border-color: #40a9ff;
  color: #40a9ff;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .device-preview {
    width: 280px;
    height: 500px;
  }
  
  .phone-frame {
    width: 240px;
    height: 450px;
  }
}
</style>